package com.qf.dao;

import com.qf.pojo.User;
import com.qf.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author 千锋健哥
 */
public class UserDaoImpl implements UserDao {

    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

    @Override
    public User findUserByUserName(String userName) {
        String sql = "select * from tb_user where username= ?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), userName);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insert(User user) {
        try {
            String sql = "insert into tb_user (username, `password`, email, gender, `status`, role, `code`) " +
                    " VALUES(?, ?, ?, ?, ?, ?, ?)";

            queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(),
                    user.getGender(), user.getStatus(), user.getRole(), user.getCode());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateStatus(String email, String code) {
        try {
            String sql = "update tb_user set status=1 where email=? and code=?";
            queryRunner.update(sql, email, code);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
